package com.wen.yygh.cmn.controller;

import com.wen.yygh.cmn.serivce.DictService;
import com.wen.yygh.common.result.Result;
import com.wen.yygh.model.cmn.Dict;
import com.wen.yygh.model.hosp.HospitalSet;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.ResultSet;
import java.util.List;

/**
 * @author Wenjl
 */
@RestController
@Api(tags = "数据字典管理")
@RequestMapping("/admin/cmn/dict")

public class DictController {
    @Autowired
    private DictService dictService;

    /**
     * 根据数据id查询子数据列表
     *
     * @return
     */
    @ApiOperation(value = "根据数据id查询子数据列表")
    @GetMapping("findChildData/{id}")
    public Result findChildData(@PathVariable("id") Long id) {
        List<Dict> dictList = dictService.findChildData(id);

        return Result.ok(dictList);
    }


    /**
     * 导出数据字典
     *
     * @param response
     */
    @GetMapping("exportData")
    @ApiOperation("导出数据字典")
    public void exportDict(HttpServletResponse response) throws IOException {
        dictService.exportDictData(response);
    }

    /**
     * 导入数据字典
     *
     * @param file
     */
    @PostMapping("importData")
    @ApiOperation("导入数据字典")
    public void importDict(MultipartFile file) {
        dictService.importDictData(file);

    }

    /**
     * 根据dictcode和value查询名称
     *
     * @return
     */
    @ApiOperation("根据dictcode和value查询名称")
    @GetMapping("getName/{dictCode}/{value}")
    public String getNameByDictCodeWithValue(@PathVariable("dictCode") String dictCode,
                                             @PathVariable("value") String value) {
        String result = dictService.findNameByDictCodeWithValue(dictCode, value);
        return result;
    }

    /**
     * 根据dictcode和value查询名称 //根据value查询
     *
     * @return
     */
   @ApiOperation("\"根据value查询名称\"")
    @GetMapping("getName/{value}")
    public String getNameByDictCodeWithValue(@PathVariable("value") String value) {
        String result = dictService.findNameByValue(value);
        return result;
    }

    @ApiOperation(value = "根据dictCode获取下级节点")
    @GetMapping(value = "/findByDictCode/{dictCode}")
    public Result getNameByDictCode(@PathVariable("dictCode") String dictCode) {
        List<Dict> result = dictService.findNameByDictCode(dictCode);
        return Result.ok(result);
    }

}
